/**
 * 功能描述 列表
 */

define(function (require, exports, module){
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var store =require("store");
	var planfile = require("../store/planfile");
	
	var instance = {
		initComponent:function (params,widget){
            var grid=new Grid({
            	parentNode:".J-grid",
            	url : "api/approvaldefine/query",
            	params:function(){
            		return {
                		fetchProperties:planfile.fetchProperties
            		};
            	},
                model:{
                    columns:[{
                    	name:"description",
                    	label:"描述",
						format:"detail",
						formatparams:{
							key:"detail",
							handler:function(index,data,rowEle){
								widget.set("pkApprovalDefine",data.pkApprovalDefine);
								var form= widget.get("form");
    							var subnav= widget.get("subnav");
    							var appGrid= widget.get("appGrid");
    							form.reset();
    							if(data.setupTime){
    								data.setupTime = moment(data.setupTime).format("YYYY-MM-DD");
    							}
								form.setData(data);
								form.setDisabled(true);
    							widget.show([".J-form",".J-appGrid"]).hide(".J-grid");
    							subnav.show(["return"]);
    							widget.set("flag",false);
    							appGrid.hideButton("add");
								appGrid.refresh({
									"approvalDefine.pkApprovalDefine":widget.get("pkApprovalDefine"),
		            				fetchProperties:"approvalDefineItem.*," +
		            						"personalInfo.*," +
		            						"approvalDefineItem.approvalRole.*," +
		            						"approvalDefineItem.approvalUser.*" ,
								});
							}
						}
                    },{
						name:"setupTime",
						label:"设置时间",
						format:"date"
					},{
						name:"user.name",
						label:"设置人"
					},{
						name:"status.value",
						label:"状态",
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"edit",
							icon:"icon-edit",
							handler:function(index,data,rowEle){
								if(data.status.key == "Setting"){
									Dialog.alert({
	        							content:"启用时无法编辑！"
	        						});
									return;
								}
								widget.set("pkApprovalDefine",data.pkApprovalDefine);
								var form= widget.get("form");
    							var subnav= widget.get("subnav");
    							var appGrid= widget.get("appGrid");
    							form.reset();
    							if(data.setupTime){
    								data.setupTime = moment(data.setupTime).format("YYYY-MM-DD");
    							}
								form.setData(data);
								form.setDisabled(true);
								appGrid.showButton("add");
    							widget.show([".J-form",".J-appGrid"]).hide(".J-grid");
    							subnav.show(["return"]);
    							widget.set("flag",true);
								appGrid.refresh({
									"approvalDefine.pkApprovalDefine":widget.get("pkApprovalDefine"),
		            				fetchProperties:"approvalDefineItem.*," +
		            						"personalInfo.*," +
		            						"approvalDefineItem.approvalRole.*," +
		            						"approvalDefineItem.approvalUser.*" ,
								});
							}
						},{
							id:"blockupbtn",
							text:"停用",
							show:function(value,row){
								if(row.status.key == "Setting"){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								Dialog.confirm({
									title:"提示",
									content:"是否停用当前审批流程？",
									confirm:function(){
										aw.ajax({
											url:"api/approvaldefine/blockup",
											data:{
												pkApprovalDefine:data.pkApprovalDefine,
												version:data.version,
												},
											dataType:"json",
											success:function(data){
												widget.get("grid").refresh();
											}
										});
										
									}
								});
							}
						},{
							id:"setup",
							text:"启用",
							show:function(value,row){
								if(row.status.key != "Setting"){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								if(data.approvalDefineItems.length==0){
									Dialog.alert({
	        							content:"该审批流程没有定义流程明细,请先定义再启用！"
	        						});
									return;
								}
								Dialog.confirm({
									title:"提示",
									content:"是否启用当前审批流程？",
									confirm:function(){
										aw.ajax({
											url:"api/approvaldefine/save",
											data:{
												pkApprovalDefine:data.pkApprovalDefine,
												status:"Setting",
												version:data.version,
												},
											dataType:"json",
											success:function(data){
												widget.get("grid").refresh();
											}
										});
										
									}
								});
							}
						}]
					}]
				}
            });
			widget.set("grid",grid);
		}
	};
	module.exports = instance;
});
